package com.ict.web.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ict.common.utils.SecurityUtils;
import com.ict.common.utils.StringUtils;
import com.ict.common.utils.bean.BeanUtils;
import com.ict.common.utils.uuid.IdUtils;
import com.ict.web.constant.ProjectStatusEnum;
import com.ict.web.controller.file.domain.YzqFile;
import com.ict.web.controller.file.dto.YzqFileDTO;
import com.ict.web.controller.file.service.IYzqFileService;
import com.ict.web.domain.*;
import com.ict.web.dto.SchemeDeclarationAddDto;
import com.ict.web.dto.SchemeDeclarationEditDto;
import com.ict.web.mapper.ProjectManagementMapper;
import com.ict.web.mapper.SchemeDeclarationMapper;
import com.ict.web.service.*;
import com.ict.web.vo.SchemeDeclarationVo;
import com.ict.web.vo.SystemInfoDetailVo;
import io.jsonwebtoken.lang.Assert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 方案申报--刘杰Service业务层处理
 *
 * @author ruoyi
 * @date 2022-02-25
 */
@Service
public class SchemeDeclarationServiceImpl extends ServiceImpl<SchemeDeclarationMapper,SchemeDeclaration> implements ISchemeDeclarationService
{
    @Resource
    private SchemeDeclarationMapper schemeDeclarationMapper;

    @Resource
    private ProjectManagementMapper projectManagementMapper;
    @Resource
    private IYzqFileService fileService;
    @Resource
    private IApprovalService approvalService;

    @Resource
    private IGovernmentDataResourcesService governmentDataResourcesService;

    @Resource
    private ICloudResourceRequirementService cloudResourceRequirementService;

    @Resource
    private IIotDeviceInfoService iotDeviceInfoService;

    @Resource
    private ISystemInfoService systemInfoService;
    @Autowired
    private IReviewTaskService reviewTaskService;
    @Autowired
    private IAnnualDeclarationService annualDeclarationService;
    @Autowired
    private ISupplementaryDeclarationService supplementaryDeclarationService;
    /**
     * 查询方案申报--刘杰
     *
     * @param programmeId 方案申报--刘杰主键
     * @return 方案申报--刘杰
     */
    @Override
    public SchemeDeclarationVo selectSchemeDeclarationByProgrammeId(Long programmeId)
    {
        SchemeDeclaration schemeDeclaration = schemeDeclarationMapper.selectSchemeDeclarationByProgrammeId(programmeId);
        SchemeDeclarationVo schemeDeclarationVo = new SchemeDeclarationVo();
        BeanUtils.copyProperties(schemeDeclaration,schemeDeclarationVo);
        if (schemeDeclaration.getResearchReport()!=null){
            YzqFile fileBp = fileService.getById(schemeDeclaration.getResearchReport());
            schemeDeclarationVo.setResearchReportFile(fileBp);
        }

        if (schemeDeclaration.getBudgetProposal()!=null) {
            YzqFile fileBp = fileService.getById(schemeDeclaration.getBudgetProposal());
            schemeDeclarationVo.setBudgetProposalFile(fileBp);
        }

        if (schemeDeclaration.getPreliminaryDesignScheme()!=null) {
            YzqFile fileBp = fileService.getById(schemeDeclaration.getPreliminaryDesignScheme());
            schemeDeclarationVo.setPreliminaryDesignSchemeFile(fileBp);
        }

        if (schemeDeclaration.getBudgetProgramme()!=null) {
            YzqFile fileBp = fileService.getById(schemeDeclaration.getBudgetProgramme());
            schemeDeclarationVo.setBudgetProgrammeFile(fileBp);
        }

        if (schemeDeclaration.getCloudResourceApplication()!=null) {
            YzqFile fileBp = fileService.getById(schemeDeclaration.getCloudResourceApplication());
            schemeDeclarationVo.setCloudResourceApplicationFile(fileBp);
        }
        //系统信息
        List<SystemInfo> systemInfoLists = systemInfoService.lambdaQuery().eq(SystemInfo::getProgrammeId, schemeDeclaration.getProgrammeId())
                .eq(SystemInfo::getDelFlag, ExtractExpert.DELETE_STATUS_NORMAL).list();
        List<SystemInfoDetailVo> arrayList = new ArrayList<>();
        systemInfoLists.stream().forEach(s->{
            SystemInfoDetailVo systemInfoDetailVo = new SystemInfoDetailVo();
            org.springframework.beans.BeanUtils.copyProperties(s,systemInfoDetailVo);
            arrayList.add(systemInfoDetailVo);
        });
        schemeDeclarationVo.setSystemInfoList(arrayList);
        //政务数据资源目录清单
        List<GovernmentDataResources> governmentDataResourcesList = governmentDataResourcesService.lambdaQuery()
                .eq(GovernmentDataResources::getProgrammeId, schemeDeclaration.getProgrammeId()).list();
        schemeDeclarationVo.setGovernmentDataResourcesAddDtoList(governmentDataResourcesList);
        //云资源需求
        List<CloudResourceRequirement> cloudResourceRequirementList = cloudResourceRequirementService.lambdaQuery()
                .eq(CloudResourceRequirement::getProgrammeId, schemeDeclaration.getProgrammeId()).list();
        schemeDeclarationVo.setInsertCloudResourceDtoList(cloudResourceRequirementList);
        //物联设备信息
        List<IotDeviceInfo> iotDeviceInfoList = iotDeviceInfoService.lambdaQuery()
                .eq(IotDeviceInfo::getProgrammeId, schemeDeclaration.getProgrammeId()).list();
        schemeDeclarationVo.setInsertIotDeviceInfoDtosList(iotDeviceInfoList);
        return schemeDeclarationVo;
    }

    /**
     * 查询方案申报--刘杰列表
     *
     * @param schemeDeclaration 方案申报--刘杰
     * @return 方案申报--刘杰
     */
    @Override
    public List<SchemeDeclaration> selectSchemeDeclarationList(SchemeDeclaration schemeDeclaration)
    {
        return schemeDeclarationMapper.selectSchemeDeclarationList(schemeDeclaration);
    }

   /**
     * 新增方案申报--刘杰
     *
     * @param dto 方案申报--刘杰
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insertSchemeDeclaration(SchemeDeclarationAddDto dto)
    {
        //关于文件是否必传的判断
        ProjectManagement management = projectManagementMapper.selectById(dto.getProId());
//        if (management.getType().equals("0")) {
//            //建设类项目
//            if (management.getInvestmentEstimation().compareTo(new BigDecimal("1000"))==-1||
//                    management.getInvestmentEstimation().compareTo(new BigDecimal("1000"))==0) {
//                //预估资金小于等于1000w的
//                Assert.isTrue(StringUtils.isNotNull(dto.getResearchReport().getFileUrl()),"请上传可行性研究报告！！！");
//                Assert.isTrue(StringUtils.isNotNull(dto.getBudgetProposal().getFileUrl()),"请上传概算方案！！！");
//            }else {
//                //预估资金大于1000w的
//                Assert.isTrue(StringUtils.isNotNull(dto.getResearchReport().getFileUrl()),"请上传可行性研究报告！！！");
//                Assert.isTrue(StringUtils.isNotNull(dto.getBudgetProposal().getFileUrl()),"请上传概算方案！！！");
//                Assert.isTrue(StringUtils.isNotNull(dto.getPreliminaryDesignScheme().getFileUrl()),"请上传初设方案！！！");
//            }
//        }else if (management.getType().equals("1")){
//            //购买类项目
//            Assert.isTrue(StringUtils.isNotNull(dto.getPreliminaryDesignScheme().getFileUrl()),"请上传初设方案！！！");
//            Assert.isTrue(StringUtils.isNotNull(dto.getBudgetProgramme().getFileUrl()),"请上传预算方案！！！");
//        }

        SchemeDeclaration bean = SchemeDeclarationAddDto.of(dto);
        bean.setUserId(SecurityUtils.getUserId());
        //同步文件对象
        ArrayList<YzqFile> fileArrayList = SchemeDeclarationAddDto.toFile(bean, dto);
        //方案申报进程资料库
        String str=null;
        if(StringUtils.isNotEmpty(fileArrayList)){
            List<String> fileIdlList = fileArrayList.stream().map(YzqFile::getId).collect(Collectors.toList());
             str=String.join(",", fileIdlList);
        }
        schemeDeclarationMapper.insert(bean);
        fileService.batchInsertFile(fileArrayList);
        //添加系统信息
        systemInfoService.insertSystemInfo(bean.getProgrammeId(), dto.getSystemInfoList());
        //添加政务数据资源目录清单
        if (dto.getGovernmentDataResourcesAddDtoList()!=null) {
            dto.getGovernmentDataResourcesAddDtoList().stream().forEach(s->{
                governmentDataResourcesService.insertGovernmentDataResources(bean.getProgrammeId(),s);
            });
        }

        //添加云资源需求
        if (dto.getInsertCloudResourceDtoList()!=null) {
            dto.getInsertCloudResourceDtoList().stream().forEach(s->{
                cloudResourceRequirementService.insertCloudResourceRequirement(bean.getProgrammeId(), s);
            });
        }

        //添加物联设备信息
        if (dto.getInsertIotDeviceInfoDtosList()!=null) {
            dto.getInsertIotDeviceInfoDtosList().stream().forEach(s->{
                iotDeviceInfoService.insertIotDeviceInfo(bean.getProgrammeId(), s);
            });
        }

        //更新进程为方案申报-已提交进程
        approvalService.dealStayApproval(management.getProId(),ProjectStatusEnum.SCHEME_DECLARATION_TO_BE_SUBMITTED.get().getStatus(),
                ProjectStatusEnum.SCHEME_DECLARATION_SUBMISSION_SUBMITTED.get().getStatus(),
                str,null,null,SecurityUtils.getNickName());
        //项目状态更新会安排评审会议待抽取专家
        management.setProgrammeId(bean.getProgrammeId());
        management.setIsState(ProjectStatusEnum.ARRANGE_REVIEW_MEETING_TO_BE_EXTRACTED.get().getStatus());
        management.setNode(ProjectStatusEnum.ARRANGE_REVIEW_MEETING_TO_BE_EXTRACTED.get().getNode());
        management.setStag(ProjectStatusEnum.ARRANGE_REVIEW_MEETING_TO_BE_EXTRACTED.get().getStage());
        //同步项目数据到项目信息表中
        approvalService.synchronizationProjectInfo(management);
        //新增安排评审会议进程
        approvalService.save(new Approval(IdUtils.snowId(),management.getProId(),ProjectStatusEnum.ARRANGE_REVIEW_MEETING_TO_BE_EXTRACTED.get().getStage(),
                ProjectStatusEnum.ARRANGE_REVIEW_MEETING_TO_BE_EXTRACTED.get().getNode(),LocalDateTime.now(),null,null,ProjectStatusEnum.ARRANGE_REVIEW_MEETING_TO_BE_EXTRACTED.get().getStatus(),
                "/",null, null,null, LocalDateTime.now(),Approval.APPROVAL_TYPE_PROJECT_MANAGE));
        //更新项目申报的项目估算
            //年度申报
        if(ProjectManagement.PROJECT_DECLARE.equals(management.getNature())){
            AnnualDeclaration annualDeclaration = annualDeclarationService.getById(management.getAnnualId());
            Assert.isTrue(StringUtils.isNotNull(annualDeclaration),"未找到项目年度申报");
            annualDeclaration.setProjectInvestmentEstimation(dto.getProjectInvestmentEstimation());
            annualDeclarationService.updateById(annualDeclaration);
            //增补申报
        }else {
            SupplementaryDeclaration supplementaryDeclaration = supplementaryDeclarationService.getById(management.getSupplementaryId());
            Assert.isTrue(StringUtils.isNotNull(supplementaryDeclaration),"未找到改增补申报信息");
            supplementaryDeclaration.setProjectInvestmentEstimation(dto.getProjectInvestmentEstimation());
            supplementaryDeclarationService.updateById(supplementaryDeclaration);
        }
        //同步评审任务节点
        management.setInvestmentEstimation(dto.getProjectInvestmentEstimation());
        projectManagementMapper.updateById(management);
        return bean.getProgrammeId();
    }

    /**
     * 修改方案申报--刘杰
     *
     * @param programmeId 方案申报--刘杰
     * @return 结果
     */
    @Override
    public int updateSchemeDeclaration(SchemeDeclarationEditDto dto,Long programmeId)
    {
        //关于文件是否必传的判断
        ProjectManagement management = projectManagementMapper.selectById(dto.getProId());
//        if (management.getType().equals("0")) {
//            //建设类项目
//            if (management.getInvestmentEstimation().compareTo(new BigDecimal("1000"))==-1||
//                    management.getInvestmentEstimation().compareTo(new BigDecimal("1000"))==0) {
//                //预估资金小于等于1000w的
//                Assert.isTrue(StringUtils.isNotNull(dto.getResearchReport().getFileUrl()),"请上传可行性研究报告！！！");
//                Assert.isTrue(StringUtils.isNotNull(dto.getBudgetProposal().getFileUrl()),"请上传概算方案！！！");
//            }else {
//                //预估资金大于1000w的
//                Assert.isTrue(StringUtils.isNotNull(dto.getResearchReport().getFileUrl()),"请上传可行性研究报告！！！");
//                Assert.isTrue(StringUtils.isNotNull(dto.getBudgetProposal().getFileUrl()),"请上传概算方案！！！");
//                Assert.isTrue(StringUtils.isNotNull(dto.getPreliminaryDesignScheme().getFileUrl()),"请上传初设方案！！！");
//            }
//        }else if (management.getType().equals("1")){
//            //购买类项目
//            Assert.isTrue(StringUtils.isNotNull(dto.getPreliminaryDesignScheme().getFileUrl()),"请上传初设方案！！！");
//            Assert.isTrue(StringUtils.isNotNull(dto.getBudgetProgramme().getFileUrl()),"请上传预算方案！！！");
//        }
        SchemeDeclaration bean = schemeDeclarationMapper.selectById(programmeId);
        BeanUtils.copyProperties(dto, bean);
        bean.setUpdateTime(LocalDateTime.now());
        //更新文件信息
        ArrayList<YzqFile> fileList = new ArrayList<>();
        if(StringUtils.isNotEmpty(bean.getResearchReport())){
            YzqFile fileRr = fileService.getById(bean.getResearchReport());
            if(StringUtils.isNotNull(fileRr)){
                YzqFileDTO fileRrDTO = dto.getResearchReport();
                BeanUtils.copyProperties(fileRrDTO,fileRr);
                fileList.add(fileRr);
            }

        }

        if(StringUtils.isNotEmpty(bean.getBudgetProposal())){
            YzqFile fileBp = fileService.getById(bean.getBudgetProposal());
            if(StringUtils.isNotNull(fileBp)){
                YzqFileDTO fileBpDTO = dto.getBudgetProposal();
                BeanUtils.copyProperties(fileBpDTO,fileBp);
                fileList.add(fileBp);
            }

        }


        if(StringUtils.isNotEmpty(bean.getBudgetProgramme())){
            YzqFile filePd = fileService.getById(bean.getBudgetProgramme());
            if(StringUtils.isNotNull(filePd)){
                YzqFileDTO filePdDTO = dto.getBudgetProgramme();
                BeanUtils.copyProperties(filePdDTO,filePd);
                fileList.add(filePd);
            }

        }


        if(StringUtils.isNotEmpty(bean.getPreliminaryDesignScheme())){
            YzqFile fileBpm = fileService.getById(bean.getPreliminaryDesignScheme());
            if(StringUtils.isNotNull(fileBpm)){
                YzqFileDTO fileBpmDTO = dto.getPreliminaryDesignScheme();
                BeanUtils.copyProperties(fileBpmDTO,fileBpm);
                fileList.add(fileBpm);
            }

        }
        if(StringUtils.isNotEmpty(bean.getCloudResourceApplication())){
            YzqFile fileBpm = fileService.getById(bean.getCloudResourceApplication());
            if(StringUtils.isNotNull(fileBpm)){
                YzqFileDTO fileCloud = dto.getCloudResourceApplication();
                BeanUtils.copyProperties(fileCloud,fileBpm);
                fileList.add(fileBpm);
            }

        }
        fileService.updateBatchById(fileList);
        //所有清单信息的修改均为先删除后增加
        List<SystemInfo> systemInfoList = systemInfoService.lambdaQuery().eq(SystemInfo::getProgrammeId, bean.getProgrammeId()).list();
        List<Long> systemInfoIdList = systemInfoList.stream().map(SystemInfo::getSysId).collect(Collectors.toList());
        systemInfoService.removeByIds(systemInfoIdList);
        if (dto.getSystemInfoList()!=null) {
            systemInfoService.insertSystemInfo(bean.getProgrammeId(), dto.getSystemInfoList());
        }
        //政务数据资源目录清单
        List<GovernmentDataResources> governmentDataResourcesList = governmentDataResourcesService.lambdaQuery()
                .eq(GovernmentDataResources::getProgrammeId, bean.getProgrammeId()).list();
        List<Long> dataIdList = governmentDataResourcesList.stream().map(GovernmentDataResources::getGovernmentDataId).collect(Collectors.toList());
        governmentDataResourcesService.removeByIds(dataIdList);
        if (dto.getGovernmentDataResourcesAddDtoList()!=null) {
            dto.getGovernmentDataResourcesAddDtoList().stream().forEach(s->{
                governmentDataResourcesService.insertGovernmentDataResources(bean.getProgrammeId(),s);
            });
        }
        //云资源需求
        List<CloudResourceRequirement> cloudResourceRequirementList = cloudResourceRequirementService.lambdaQuery()
                .eq(CloudResourceRequirement::getProgrammeId, bean.getProgrammeId()).list();
        List<Long> cloudIdList = cloudResourceRequirementList.stream().map(CloudResourceRequirement::getCloudResourceId).collect(Collectors.toList());
        cloudResourceRequirementService.removeByIds(cloudIdList);
        if (dto.getInsertCloudResourceDtoList()!=null) {
            dto.getInsertCloudResourceDtoList().stream().forEach(s->{
                cloudResourceRequirementService.insertCloudResourceRequirement(bean.getProgrammeId(), s);
            });
        }
        //物联设备信息
        List<IotDeviceInfo> iotDeviceInfoList = iotDeviceInfoService.lambdaQuery()
                .eq(IotDeviceInfo::getProgrammeId, bean.getProgrammeId()).list();
        List<Long> lotIdList = iotDeviceInfoList.stream().map(IotDeviceInfo::getLotDeviceId).collect(Collectors.toList());
        iotDeviceInfoService.removeByIds(lotIdList);
        if (dto.getInsertIotDeviceInfoDtosList()!=null) {
            dto.getInsertIotDeviceInfoDtosList().stream().forEach(s->{
                iotDeviceInfoService.insertIotDeviceInfo(bean.getProgrammeId(), s);
            });
        }

        List<Approval> collect = approvalService.lambdaQuery().eq(Approval::getApprovalType,Approval.APPROVAL_TYPE_PROJECT_MANAGE)
                .eq(Approval::getProjectId, management.getProId()).list().stream()
                .sorted(Comparator.comparing(Approval::getCreateTime).reversed()).collect(Collectors.toList());
        Assert.isTrue(StringUtils.isNotEmpty(collect),"未找到申报修改相关的审核记录");
        Approval approval = new Approval();
        if (StringUtils.isNotNull(management.getTripartiteId())) {
             approval = collect.get(1);
        }else{
             approval = collect.get(0);
        }

            //方案重申（现场论证、方案预审、意见汇总的退回重申）：新增安排评审会议进程、更新项目、评审任务、项目信息表信息
        if(ProjectStatusEnum.ON_SITE_DEMONSTRATION_RETURN_RESTATEMENT.get().getStatus().equals(approval.getApprovalStatus())
            ||ProjectStatusEnum.PROGRAM_PREREVIEW_RETURN_FOR_RETRIAL.get().getStatus().equals(approval.getApprovalStatus())
            ||ProjectStatusEnum.SUMMARY_AND_RETURN_OF_COMMENTS.get().getStatus().equals(approval.getApprovalStatus())){
            Long reviewTaskId = management.getReviewTaskId();
            Assert.isTrue(StringUtils.isNotNull(reviewTaskId),"项目未关联相关评审任务");
            ReviewTask reviewTask = reviewTaskService.getById(reviewTaskId);
            approval.setEndTime(LocalDateTime.now());
            approvalService.updateById(approval);

            //新增项目在线预审待安排评审会议进程
            approvalService.save(new Approval(IdUtils.snowId(),management.getProId(),ProjectStatusEnum.ONLINE_PREQUALIFICATION_PENDING_ARRANGE.get().getStage(),
                    ProjectStatusEnum.ONLINE_PREQUALIFICATION_PENDING_ARRANGE.get().getNode(),LocalDateTime.now(),LocalDateTime.now(),null,ProjectStatusEnum.ONLINE_PREQUALIFICATION_PENDING_ARRANGE.get().getStatus(),
                    "/",null, null,null, LocalDateTime.now(),Approval.APPROVAL_TYPE_PROJECT_MANAGE));
            management.setIsState(ProjectStatusEnum.ONLINE_PREQUALIFICATION_PENDING_ARRANGE.get().getStatus());
            management.setNode(ProjectStatusEnum.ONLINE_PREQUALIFICATION_PENDING_ARRANGE.get().getNode());
            management.setStag(ProjectStatusEnum.ONLINE_PREQUALIFICATION_PENDING_ARRANGE.get().getStage());
            //同步评审任务节点
            reviewTask.setNode(ProjectStatusEnum.ONLINE_PREQUALIFICATION_PENDING_ARRANGE.get().getNode());
            reviewTask.setNodeStatus(ProjectStatusEnum.ONLINE_PREQUALIFICATION_PENDING_ARRANGE.get().getStatus());
            reviewTaskService.updateById(reviewTask);
            //同步项目数据到项目信息表中
            approvalService.synchronizationProjectInfo(management);
        //方案重申(大数据局、三方机构返回项目单位的退回重申)：更新方案申报、项目单位，新增待安排评审会议进程
        }else if(ProjectStatusEnum.SCHEME_DECLARATION_TO_BE_SUBMITTED.get().getStatus().equals(approval.getApprovalStatus())){
            List<Approval> approvalList = approvalService.lambdaQuery().eq(Approval::getProjectId, management.getProId())
                    .eq(Approval::getApprovalType, Approval.APPROVAL_TYPE_PROJECT_MANAGE)
                    .orderByDesc(Approval::getCreateTime).list();
            Approval approvalSubmitted = approvalList.get(0);
            approvalSubmitted.setEndTime(LocalDateTime.now());
            approvalSubmitted.setApprovalStatus(ProjectStatusEnum.SCHEME_DECLARATION_SUBMISSION_SUBMITTED.get().getStatus());
            approvalSubmitted.setApprovalOperator(String.valueOf(SecurityUtils.getNickName()));
            approvalService.updateById(approvalSubmitted);
            //新增项目在线预审待安排评审会议进程
            approvalService.save(new Approval(IdUtils.snowId(),management.getProId(),ProjectStatusEnum.ARRANGE_REVIEW_MEETING_TO_BE_EXTRACTED.get().getStage(),
                    ProjectStatusEnum.ARRANGE_REVIEW_MEETING_TO_BE_EXTRACTED.get().getNode(),LocalDateTime.now(),LocalDateTime.now(),null,ProjectStatusEnum.ARRANGE_REVIEW_MEETING_TO_BE_EXTRACTED.get().getStatus(),
                    "/",null, null,null, LocalDateTime.now(),Approval.APPROVAL_TYPE_PROJECT_MANAGE));

            management.setIsState(ProjectStatusEnum.ARRANGE_REVIEW_MEETING_TO_BE_EXTRACTED.get().getStatus());
            management.setNode(ProjectStatusEnum.ARRANGE_REVIEW_MEETING_TO_BE_EXTRACTED.get().getNode());
            management.setStag(ProjectStatusEnum.ARRANGE_REVIEW_MEETING_TO_BE_EXTRACTED.get().getStage());
        }else {
            //方案修改
            List<Approval> collect1 = approvalService.lambdaQuery().eq(Approval::getApprovalNode,ProjectStatusEnum.ON_SITE_DEMONSTRATION_NEEDS_TO_BE_MODIFIED.get().getNode())
                    .eq(Approval::getApprovalType,Approval.APPROVAL_TYPE_PROJECT_MANAGE)
                    .eq(Approval::getProjectId, management.getProId()).list().stream()
                    .sorted(Comparator.comparing(Approval::getCreateTime).reversed()).collect(Collectors.toList());
            if(StringUtils.isNotEmpty(collect1)){
                //方案修改：更新审核进程、项目、评审任务状态为方案修改审核待审核
                if(ProjectStatusEnum.ON_SITE_DEMONSTRATION_NEEDS_TO_BE_MODIFIED.get().getStatus().equals(collect1.get(0).getApprovalStatus())){
                    //修改上一个方案修改审核进程的结束时间
                    Approval approval1 = approvalService.getApproval( management.getProId(),  ProjectStatusEnum.SCHEME_DECLARATION_UPDATE_RETURN_MODIFICATION.get().getStatus(), null,  null);
                    if(StringUtils.isNotNull(approval1)){
                        approval1.setEndTime(LocalDateTime.now());
                        approvalService.updateById(approval1);
                    }
                    //现场论证审核进程更新结束时间
                    Approval toModifiedApproval = approvalService.getApproval( management.getProId(),  ProjectStatusEnum.ON_SITE_DEMONSTRATION_NEEDS_TO_BE_MODIFIED.get().getStatus(), null,  null);
                    toModifiedApproval.setEndTime(LocalDateTime.now());
                    approvalService.updateById(toModifiedApproval);
                    ReviewTask reviewTask = reviewTaskService.getById(management.getReviewTaskId());
                    reviewTask.setNode(ProjectStatusEnum.SCHEME_DECLARATION_UPDATE_TO_BE_APPROVED.get().getNode());
                    reviewTask.setNodeStatus(ProjectStatusEnum.SCHEME_DECLARATION_UPDATE_TO_BE_APPROVED.get().getStatus());
                    reviewTaskService.updateById(reviewTask);
                    management.setIsState(ProjectStatusEnum.SCHEME_DECLARATION_UPDATE_TO_BE_APPROVED.get().getStatus());
                    management.setNode(ProjectStatusEnum.SCHEME_DECLARATION_UPDATE_TO_BE_APPROVED.get().getNode());
                    management.setStag(ProjectStatusEnum.SCHEME_DECLARATION_UPDATE_TO_BE_APPROVED.get().getStage());
                    //同步项目数据到项目信息表中
                    approvalService.synchronizationProjectInfo(management);

                    approvalService.save(new Approval(IdUtils.snowId(),management.getProId(),ProjectStatusEnum.SCHEME_DECLARATION_UPDATE_TO_BE_APPROVED.get().getStage(),
                            ProjectStatusEnum.SCHEME_DECLARATION_UPDATE_TO_BE_APPROVED.get().getNode(),LocalDateTime.now(),LocalDateTime.now(),null,ProjectStatusEnum.SCHEME_DECLARATION_UPDATE_TO_BE_APPROVED.get().getStatus(),
                            "/",null, null,null, LocalDateTime.now(),Approval.APPROVAL_TYPE_PROJECT_MANAGE));
                }
            }
        }
        //更新项目申报的项目估算
        //年度申报
        if(ProjectManagement.PROJECT_DECLARE.equals(management.getNature())){
            AnnualDeclaration annualDeclaration = annualDeclarationService.getById(management.getAnnualId());
            Assert.isTrue(StringUtils.isNotNull(annualDeclaration),"未找到项目年度申报");
            annualDeclaration.setProjectInvestmentEstimation(dto.getProjectInvestmentEstimation());
            annualDeclarationService.updateById(annualDeclaration);
            //增补申报
        }else {
            SupplementaryDeclaration supplementaryDeclaration = supplementaryDeclarationService.getById(management.getSupplementaryId());
            Assert.isTrue(StringUtils.isNotNull(supplementaryDeclaration),"未找到改增补申报信息");
            supplementaryDeclaration.setProjectInvestmentEstimation(dto.getProjectInvestmentEstimation());
            supplementaryDeclarationService.updateById(supplementaryDeclaration);
        }
        management.setInvestmentEstimation(dto.getProjectInvestmentEstimation());
        projectManagementMapper.updateById(management);
        return schemeDeclarationMapper.updateById(bean);
    }

    /**
     * 删除方案申报--刘杰信息
     *
     * @param programmeId 方案申报--刘杰主键
     * @return 结果
     */
    @Override
    public int deleteSchemeDeclarationByProgrammeId(Long programmeId)
    {
        SchemeDeclaration bean = schemeDeclarationMapper.selectById(programmeId);
        bean.setDelFlag("1");
        return schemeDeclarationMapper.updateById(bean);
    }
}
